import ouApi from "@/api/activeDirectory/ou";

//定义一个混入对象
export const orgMixin = {
  data() {
    return {
      treeProps: {
        label: "label",
        children: "children",
        isLeaf: "isLeaf"
      },
      ouTree: [],
      rootNode: {},
      expandKeys: []
    };
  },
  components: {},
  created: function() {
    this.getTreeselect();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      ouApi.tree().then(response => {
        this.ouTree = response.data;
        this.$nextTick(() => {
          this.rootNode = this.ouTree[0];
          const currentKey = this.ouTree[0].id;
          this.expandKeys = [currentKey];
          if (this.$refs["tree"]) {
            this.$refs["tree"].setCurrentKey(currentKey);
            const currentNode = this.$refs["tree"].getCurrentNode();
            this.handleNodeClick(currentNode);
          }
        });
      });
    },
    //加载子树
    getSubTree(node, resolve) {
      if (node.level == 0) {
        resolve(this.ouTree);
      } else if (node.level == 1) {
        resolve(node.data.children);
      } else if (node.level > 1) {
        if (node.data) {
          const { data } = node;
          ouApi.subList({ dn: data.id }).then(response => {
            resolve(response.data);
          });
        }
      }
    },
    // 节点单击事件
    handleNodeClick(node) {
      if (this.queryParams) this.queryParams.parentCn = node.data.cn;
      if (this.getList) this.getList();
    },

    //通用
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
